import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@views/Login.vue";
import Register from "@views/Register.vue";
import Home from "@views/Home.vue";
import UserInfo from "@views/userInfo/index.vue";
import UserDetail from "@views/userInfo/UserDetail.vue";
import UserDepartment from "@views/userInfo/UserDepartment.vue";
import UserRoleManage from "@views/userInfo/UserRoleManage.vue";
import UserModify from "@views/userInfo/UserModify.vue";
import UserDetailUpdate from "@views/userInfo/UserDetailUpdate.vue";
// 新加
import AddressList from "@views/addressList/index";
import Mine from "@views/mine/index";
import WorkSpace from "@views/workSpace/index"

import store from "../store/index"

Vue.use(VueRouter);

VueRouter.prototype.goBack = function () {
  this.isBack = true; //判断是否是返回操作
  window.history.go(-1);
};
export const routes = [{
    path: "/",
    name: "/",
    redirect: "/addressList"
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/register",
    name: "Register",
    component: Register
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      requireAuth: true
    },
    children: [
      // 新加
      {
        path: "/addressList",
        name: "AddressList",
        component: AddressList,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/workSpace",
        name: "WorkSpace",
        component: WorkSpace,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/mine",
        name: "Mine",
        component: Mine,
        meta: {
          requireAuth: true
        }
      }
    ]
  },
  {
    path: "/userInfo",
    name: "UserInfo",
    component: UserInfo,
    meta: {
      requireAuth: true
    },
    children: [{
        path: "/userInfo/detail/:id",
        name: "UserDetail",
        component: UserDetail,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/userInfo/department/:id",
        name: "UserDepartment",
        component: UserDepartment,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/userInfo/roleManage/:id",
        name: "UserRoleManage",
        component: UserRoleManage,
        meta: {
          requireAuth: true
        }
      }
    ]
  },
  {
    path: "/userInfo/modify/:id",
    name: "UserModify",
    component: UserModify,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/userInfo/UserDetailUpdate",
    name: "UserDetailUpdate",
    component: UserDetailUpdate,
    meta: {
      requireAuth: true
    }
  }
];

const router = new VueRouter({
  mode: "history",
  routes
});

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (store.getters.token) {
      next();
    } else {
      next({
        path: "/login"
      });
    }
  } else {
    next();
  }
});

export default router;
